package com.example.piece.net

import com.example.base.BaseApp
import com.example.base.tools.versionCode
import com.example.base.tools.versionName
import com.example.piece.App
import okhttp3.*
import okio.IOException
import org.json.JSONException
import org.json.JSONObject
import java.lang.String
import java.util.*
import java.util.concurrent.locks.ReentrantLock
import kotlin.Throws


/**  Token 头部拦截器, 全局统一添加 token  **/
class AddHeadInterceptor() : Interceptor {

    private val lock = ReentrantLock()

    @Throws(IOException::class)
    @Synchronized
    override fun intercept(chain: Interceptor.Chain): Response {
        synchronized(TokenInterceptor::class.java) {

            try {
                lock.lock()
                var request: Request = chain.request()
                request = addHeaderFields(request)
                request = modifyRequestBody(request)
                return chain.proceed(request)

            } finally {
                lock.unlock()
            }
        }
    }

    /**
     * add new Headers
     */
    private fun addHeaderFields(request: Request): Request {
        return request.newBuilder()
            .addHeader("Content-Type", "application/x-www-form-urlencoded")
            .addHeader(
                "uk", UUID.randomUUID().toString().replace("-", "")
                /*" 28c1697f82fc236749534f494c85db948"*/
            )
            .addHeader("channel", "cretin_open_api")
            .addHeader(
                "app", /*"3.0.2;302;12"*/
                BaseApp.context.versionName + ";" + BaseApp.context.versionCode + ";" + android.os.Build.VERSION.SDK_INT
            )
            .addHeader(
                "device",
                android.os.Build.MANUFACTURER + ";" + android.os.Build.MODEL/*"Xiaomi;zeus;cretin"*/
            )
            .addHeader("project_token", "1DB6D38F1E9D4192AF403A372FC627E0")
            .addHeader("token", App.USER_TOKEN)
            .build()
    }

    /**
     * add new post fields
     */
    private fun modifyRequestBody(request: Request): Request {
        var request = request
        if ("POST" == request.method) {
            if (request.body is FormBody||true) {
                val bodyBuilder = FormBody.Builder()
                var formBody:FormBody
                if (request.body is FormBody){
                     formBody = request.body as FormBody
                    // Copy the original parameters first
                    for (i in 0 until formBody!!.size) {
                        bodyBuilder.addEncoded(formBody.encodedName(i), formBody.encodedValue(i))
                    }
                }

                // Add common parameters
                formBody = bodyBuilder
                   /* .addEncoded("sign", "78c0ffd4b85e13f272e9dd94cd5e0757")
                    .addEncoded("time", "1657331361")
                    .addEncoded("sign", "bc09c7a186ebfe3d11e5d990209a428c")
                    .addEncoded("time", "1657421823")*/
                    .build()
                request = request.newBuilder().post(formBody).build()
            }
        }
        return request
    }
}











